<?php
include "header.php";
if($_COOKIE['userrole']!="1")
{
    die("admin need");
}
?>

<div class="container-fluid">
    <!--      <div class="row">-->
    <!--        <div class="col-sm-3 col-md-2 sidebar">-->
    <!--          <ul class="nav nav-sidebar">-->
    <!--            <li class="active">-->
    <!--                <a href="#">公云<span class="sr-only">(current)</span></a>-->
    <!--            </li>-->
    <!--            <li><a href="#">熙地港</a></li>-->
    <!--            <li><a href="#">望京凯德</a></li>-->
    <!--          </ul>-->
    <!---->
    <!--        </div>-->
    <div class="col-sm-12  col-md-12  main">
        <h1 class="page-header">角色
            <button id="btn_cancelCreateRole" type="button" class="btn btn-lg btn-warning pull-right" onclick="cancelCreateRole()" style="display: none">取消</button>
            <button id="btn_finish" type="button" class="btn btn-lg btn-success pull-right" onclick="finishCreateRole()" style="display: none">保存</button>
            <button id="btn_modify" type="button" class="btn btn-lg btn-success pull-right" onclick="finishModifyRole()" style="display: none">修改</button>
            <button id="btn_create" type="button" class="btn btn-lg btn-success pull-right" onclick="showCreateRoleDiv()">创建</button>

        </h1>

        <div style="padding: 100px 100px 10px;display: none" id="div_createRole">
            <form class="bs-example bs-example-form" role="form">

                <div class="input-group">
                    <span class="input-group-addon">角色名称</span>
                    <input id="input_name" type="text" class="form-control" placeholder="请输入角色名称" onblur="checkWidgetEcho(this.id,'role','name',this.value,current_modifyrole_id,'角色名')">
                </div>
                <br>


                <div class="input-group">
                    <span class="input-group-addon">角色描述</span>
                    <input id="input_description" type="text" class="form-control" placeholder="请输入描述以便管理员创建用户时候分辨">
                </div>
                <br>

                <div class="input-group">
                    <span class="input-group-addon">可管理的商场</span>

                    <select id="select_merchant" class="cls_select2_mt form-control" name="select_merchant">

                    </select>

                </div>
                <br>


            </form>
        </div>

        <div class="table-responsive">
            <table class="table table-striped " id="roleTable" >

            </table>
        </div>
    </div>
</div>
</div>


<script>
    var tableurl="api.php?method=getRolesTable";
    var $table = $('#roleTable');
    $table.bootstrapTable({
        //url: "merchantTable.json",
        url:tableurl,
        dataType: "json",
        pagination: true, //分页
        singleSelect: false,
        search: true, //显示搜索框
        pageSize:10,
        columns: [
            {
                title: 'ID',
                field: 'id',
                align: 'left',
                valign: 'middle'
            },
            {
                title: '角色名',
                field: 'name',
                align: 'left',
                valign: 'middle'
            },
            {
                title: '描述',
                field: 'desc',
                align: 'left',
                valign: 'middle'
            },
            {
                title: '操作',
                field: 'id',
                align: 'left',
                formatter:function(value,row,index){
                    s=e=d="";
                        e='<a href="#" mce_href="#" onclick="editR(\''+ row.id + '\',\''+row.name+'\',\''+row.desc+'\')">编辑 </a>'

                        d = '<a href="#" mce_href="#" onclick="delR(\''+ row.id +'\')">删除</a> ';

                    return s+e+d;
                }
            }
        ]
    });


    $('.cls_select2').select2({width:"100%"});
    $('.cls_select2_50').select2({width:"50%"});
    $('.cls_select2_mt').select2({width:"100%",multiple: true});

    getMerchants();

    function getMerchants()
    {
        $.ajax({
            url:'api.php?method=getMerchantsSelect',
            type:'POST', //GET
            async:true,    //或false,是否异步
            timeout:5000,    //超时时间
            dataType:'json',    //返回的数据格式：json/xml/html/script/jsonp/text
            beforeSend:function(xhr){
                console.log(xhr)
                console.log('发送前')
            },
            success:function(data,textStatus,jqXHR){
                //alert(data.name);
                for(o in data)
                {
                    if(data[o].id==undefined)continue;
                    $("#select_merchant").append("<option value='" + data[o].id + "'>&nbsp;" + data[o].name + "</option>");

                }



            },
            error:function(xhr,textStatus){
                console.log('错误')
                console.log(xhr)
                console.log(textStatus)
            },
            complete:function(){
                console.log('结束')
            }
        });
    }


    current_modifyrole_id=0;
    function editR(id,name,description)
    {
        //leak floor_info, ,roles
        current_modifyrole_id = id;
        $("#input_name").val(name);
        $("#input_description").val(description);


        getRolesMerchant(current_modifyrole_id);

        showModifyRoleDiv();
    }

    function  getRolesMerchant(rid)
    {
        $.ajax({
            url:'api.php?method=getRolesMerchant',
            data:{
                role_id:rid
            },
            type:'POST', //GET
            async:true,    //或false,是否异步
            timeout:5000,    //超时时间
            dataType:'json',    //返回的数据格式：json/xml/html/script/jsonp/text
            beforeSend:function(xhr){
                console.log(xhr)
                console.log('发送前')
            },
            success:function(data,textStatus,jqXHR){
                //alert(data.name);
                arrselectedmerchant=new Array();
                for(o in data)
                {
                    if(data[o].id==undefined)continue;
                    arrselectedmerchant.push(data[o].id)

                }
                $("#select_merchant").val(arrselectedmerchant).trigger("change");



            },
            error:function(xhr,textStatus){
                console.log('错误')
                console.log(xhr)
                console.log(textStatus)
            },
            complete:function(){
                console.log('结束')
            }
        });
    }

    function delR(rid)
    {
        bootbox.prompt("<font color='red'>确定要删除此角色吗？</font>（此操作不可撤销！如需删除请在下方输入“确认删除”）",function (result) {
            if(result=="确认删除")
            {
                $.ajax({
                    url:'api.php?method=delRole',
                    type:'POST', //GET
                    async:true,    //或false,是否异步
                    data:{
                        role_id:rid
                    },
                    timeout:5000,    //超时时间
                    dataType:'json',    //返回的数据格式：json/xml/html/script/jsonp/text
                    beforeSend:function(xhr){
                        console.log(xhr)
                        console.log('发送前')
                    },
                    success:function(data,textStatus,jqXHR){
                        //alert(data.name);
                        if(data.return_code=="0")
                        {

                            bootbox.alert("删除成功",function () {

                                $("#roleTable").bootstrapTable('refresh',{'url':tableurl});
                            });
                        }
                        else
                        {
                            if(data.return_code=="-2")
                                bootbox.alert("删除角色前，请清空其可管理商场");
                        }



                    },
                    error:function(xhr,textStatus){
                        console.log('错误')
                        console.log(xhr)
                        console.log(textStatus)
                    },
                    complete:function(){
                        console.log('结束')
                    }
                });
            }
        })
    }

    function showCreateRoleDiv()
    {
        $("#div_createRole").show();
        $("#btn_cancelCreateRole").show();
        $("#btn_finish").show();
        $("#btn_create").hide();
        current_modifyrole_id=0;
        $(".table-responsive").hide();
    }

    function cancelCreateRole()
    {
        $("#div_createRole").hide();
        $("#btn_cancelCreateRole").hide();
        $("#btn_finish").hide();
        $("#btn_modify").hide();
        $("#btn_create").show();

        $("#input_name").val("");
        $("#input_description").val("");

        $("#select_merchant").val("").trigger("change");
        //$("#select").val("").trigger("change");
        $(".table-responsive").show();

    }

    function showModifyRoleDiv()
    {
        $("#div_createRole").show();
        $("#btn_cancelCreateRole").show();
        $("#btn_modify").show();
        $("#btn_create").hide();
        $(".table-responsive").hide();



    }

    function checkRoleInput()
    {
        name = $("#input_name").val();


        desc = $("#input_description").val();

        merchant=$("#select_merchant").val();



        if(name=="")
        {
            bootbox.alert("角色名称不能为空");
            return false;
        }
        else if(desc == "")
        {
            bootbox.alert("描述不能为空",function () {

            })
            return false;
        }

        //alert(industrydetail);
        return true;
    }

    function finishModifyRole()
    {
        bcheck=checkRoleInput();
        if(!bcheck)return;
        bootbox.confirm("确定修改此角色的信息吗？",function (result) {

            if(result)
                $.ajax({
                    url:'api.php?method=modifyRole',
                    type:'POST', //GET
                    async:true,    //或false,是否异步
                    data:{
                        id:current_modifyrole_id,
                        name:name,
                        description:desc,
                        merchant:merchant
                    },
                    timeout:5000,    //超时时间
                    dataType:'json',    //返回的数据格式：json/xml/html/script/jsonp/text
                    beforeSend:function(xhr){
                        console.log(xhr)
                        console.log('发送前')
                    },
                    success:function(data,textStatus,jqXHR){
                        //alert(data.name);
                        if(data.return_code=="0")
                        {
                            bootbox.alert("修改成功",function () {
                                cancelCreateRole();
                                $("#roleTable").bootstrapTable('refresh',{'url':tableurl});
                            });
                        }
                        else
                        {
                            alert(data.return_msg);
                        }



                    },
                    error:function(xhr,textStatus){
                        console.log('错误')
                        console.log(xhr)
                        console.log(textStatus)
                    },
                    complete:function(){
                        console.log('结束')
                    }
                });
        })

    }

    function finishCreateRole()
    {
        bcheck=checkRoleInput();
        if(!bcheck)return;

        //return;

        $.ajax({
            url:'api.php?method=addRole',
            type:'POST', //GET
            async:true,    //或false,是否异步
            data:{
                name:name,
                description:desc,
                merchant:merchant

            },
            timeout:5000,    //超时时间
            dataType:'json',    //返回的数据格式：json/xml/html/script/jsonp/text
            beforeSend:function(xhr){
                console.log(xhr)
                console.log('发送前')
            },
            success:function(data,textStatus,jqXHR){
                //alert(data.name);
                if(data.return_code=="0")
                {
                    bootbox.alert("创建成功",function () {
                        cancelCreateRole();
                        $("#roleTable").bootstrapTable('refresh',{'url':tableurl});
                    });
                }
                else
                {
                    if(data.return_code=="-3")
                    {
                        bootbox.alert("角色名重复，请重新输入！");
                        return;
                    }

                }



            },
            error:function(xhr,textStatus){
                console.log('错误')
                console.log(xhr)
                console.log(textStatus)
            },
            complete:function(){
                console.log('结束')
            }
        });

    }


</script>
</body>
</html>
